package com.dxs.Service.Impl;

import com.dxs.Entity.EntityUserData;
import com.dxs.Mapper.FilesMapper;
import com.dxs.Service.FileService;
import com.dxs.Util.CreateSnowID;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.util.Objects;

@Service
public class FileServiceImpl implements FileService {

    @Resource
    private FilesMapper filesMapper;

    /**
     * 上传临时文件
     * @param file
     * @return
     * @throws IOException
     */
    @Override
    public String uploadFileTemp(MultipartFile file) throws IOException {
        Long snowID = CreateSnowID.getNewIdBySnow();
        File fileTemp = new File("pointData/tempData");
        if (!fileTemp.exists()) fileTemp.mkdirs();
        String filePath = fileTemp.getCanonicalPath();
        System.out.println("当前要传的文件夹为："+ filePath);
        String fileName = snowID + file.getOriginalFilename();
        System.out.println("文件名为："+ filePath+File.separator+fileName);
        try (BufferedOutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(new File(filePath + File.separator + fileName).toPath()));){
            outputStream.write(file.getBytes());
            outputStream.flush();
            return "http://120.48.91.193" + ":7788/pointData/tempData/"+fileName;
        }catch (IOException e){
            System.out.println("上传文件失败"+e.getMessage());
            return "";
        }
    }

    @Override
    public String uploadFileFormal(MultipartFile file, String uname, int uid, String dataMakerID, int typeCode) throws IOException {
        Long snowID = CreateSnowID.getNewIdBySnow();
        File folder = new File("pointData/data");
        if (!folder.exists()) folder.mkdirs();
        String filePath = folder.getCanonicalPath();
        System.out.println("当前要传的文件夹为："+ filePath);
        String fileName = snowID + file.getOriginalFilename();
        String filePathName = filePath + File.separator + fileName;
        System.out.println("文件名为："+ filePath+File.separator+fileName);
        try (BufferedOutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(new File(filePathName).toPath()));){
            outputStream.write(file.getBytes());
            outputStream.flush();
            String url = "http://120.48.91.193" + ":7788/pointData/data/"+fileName;
            //记录数据库
            EntityUserData userData = new EntityUserData(uname,fileName,url,"要保存的框选记录", uid, dataMakerID, typeCode);
            if(filesMapper.uploadFile(userData) < 0) throw  new RuntimeException("插入数据库失败，请清理已保存的文件");
            return url;
        }catch (IOException e){
            System.out.println("上传文件失败"+e.getMessage());
            return "";
        }
    }

    @Override
    public boolean getDataByMakerID(String dataMakerID) {
        EntityUserData result = filesMapper.getCountByDataMakerID(dataMakerID);
        return result != null;
    }

    @Override
    public String saveUserData(EntityUserData userData) {
        System.out.println(userData);
        if (Objects.equals(userData.getDataMakerID(), "")) {
            System.out.printf("新增1");
            //新增
            long DataMakerID = CreateSnowID.getNewIdBySnow();
            userData.setDataMakerID(String.valueOf(DataMakerID));
            return filesMapper.uploadFile(userData)>0 ? Long.toString(DataMakerID) : "";
        }else {
            if ((getDataByMakerID(userData.getDataMakerID()))){
                //修改
                System.out.printf("修改不是新增");
                return filesMapper.updateUserDataByDataMakerID(userData) >0 ? userData.getDataMakerID() : "";
            }else {
                //新增
                System.out.println("新增2");
                long DataMakerID = CreateSnowID.getNewIdBySnow();
                userData.setDataMakerID(String.valueOf(DataMakerID));
                return filesMapper.uploadFile(userData)>0 ? Long.toString(DataMakerID) : "";
            }
        }
    }


}
